package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.AutoBrightnessParam;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.Objects;

/**
 * 设置自动亮度
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetAutoBrightness implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 自动亮度参数
         */
        private AutoBrightnessParam param;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 自动亮度参数
         */
        private AutoBrightnessParam param;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SetAutoBrightness.InputMsg.Builder inputMsgBuilder =
                CompleteInstruction.SetAutoBrightness.InputMsg.newBuilder();
        SetAutoBrightness.InputMsg thisInput = this.getInputMsg();
        inputMsgBuilder.setParam(thisInput.getParam().toProto());

        final CompleteInstruction.SetAutoBrightness.Builder builder =
                CompleteInstruction.SetAutoBrightness.newBuilder();
        builder.setInput(inputMsgBuilder);
        return builder.build().toByteString();
    }

    @Override
    public SetAutoBrightness byteConvertObject(ByteString bytes) {
        CompleteInstruction.SetAutoBrightness setAutoBrightness = null;
        try {
            setAutoBrightness = CompleteInstruction.SetAutoBrightness.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(setAutoBrightness)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        SetAutoBrightness.OutputMsg outputMsg = new SetAutoBrightness.OutputMsg();
        outputMsg.setParam(new AutoBrightnessParam().formProto(setAutoBrightness.getOutput().getParam()));
        this.setOutputMsg(outputMsg);
        return this;
    }
}

